<?php
/* 
 *赠品 规则管理
 */
class Gift_ruleAction extends CommonAction {
    public function _filter(&$map) {
        $key = trim($_REQUEST['keywords']);
        if(!empty($key)) {
            $where['Common_product.name'] = array('like',"%".$key."%");//url
            $where['out_number'] = array('like',"%".$key."%");//url
            $where['_logic'] = "or";
            $map['_complex'] = $where;
        }
        if( $_REQUEST['order_source_id'] != '' )
          $map['order_source_id'] = $_REQUEST['order_source_id'];
    }

    public function index()
    {
        parent::index('Gift_ruleView');
    }

    public function _before_insert(){
        $_POST['common_product_id'] = $_POST['common_id'];
        if(empty($_POST['common_id']))
            $this->error("请选择产品！");
        if(empty($_POST['number']) || !is_numeric($_POST['number']) )
            $this->error("请输入正确的产品数量！");
        $con = array(
            'my_number' =>  $_POST['common_product_id'],
            'order_source_id' => $_POST['order_source_id'], //暂不取，目前仅对药房网 2013-06-07
            );
        $rs = M('Product_map')->where($con)->find();
        if(is_array($rs))
            $_POST['out_number'] = $rs['out_number']; //存入第三方编码 //未必所有平台都需要映射
        $map = array(
            'common_product_id' =>  $_POST['common_product_id'],  //其他平台有可能仅对应货品，不对应商品。药房网三个平台对应产品ID
            'order_source_id' => $_POST['order_source_id'],
            );
        $other_rule = M('Gift_rule')->where($map)->find();
        if(is_array($other_rule))
            $this->error('已存在赠品规则，请更换产品！');
    
    }

    public function _before_update(){
        $_POST['common_product_id'] = $_POST['common_id'];
        $con = array(
            'my_number' =>  $_POST['common_product_id'],
            //'order_source_id' => $_POST['order_source_id'], //暂不取，目前仅对药房网 2013-06-07
            );
        $rs = M('Product_map')->where($con)->find();
        if(is_array($rs))
            $_POST['out_number'] = $rs['out_number']; //存入第三方编码
        //控制重复赠品规则
        $map = array(
            'common_product_id' =>  $_POST['common_product_id'],
            'order_source_id' => $_POST['order_source_id'],
            'id' => array('neq',$_POST['id']), //非本ID 
            );
        $other_rule = M('Gift_rule')->where($map)->find();
        if(is_array($other_rule))
            $this->error('已存在赠品规则，请更换产品！');
    
    }


}
?>
